//
//  GetAndTopViewController.m
//  PatientApp
//
//  Created by chirui on 2017/5/18.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "GetAndTopViewController.h"
#import "GetMoneyDetailsCell.h"

@interface GetAndTopViewController ()<UITableViewDelegate,UITableViewDataSource> {
    
    UITableView * _tableView;
    
    NSString * typeStr;
    UIButton * getBtn;
    UIButton * outBtn;
    
    UIView * lineView_one;
    UIView * lineView_two;
    
    NSMutableArray * _dataArray;
    NSString * type;
    int _page;
    int _totailPage;
    
    UIView * noBodyView;
    
}

@end

@implementation GetAndTopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = @[].mutableCopy;
    self.view.backgroundColor =[UIColor whiteColor];
    TitleView(@"收支明细");
    
    typeStr = @"1";
    _page = 1;
    type = @"in";
    [self creatTableView];
    
    [self getListData];
    
     noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-150, 200, 200) andMessageStr:@"没有相关记录！" onView:nil];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        [self getListData];
        
    }];
    _tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getListData];
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    _tableView.mj_footer.hidden = NO;
    
    [_tableView registerNib:[UINib nibWithNibName:[GetMoneyDetailsCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[GetMoneyDetailsCell reuseIdentifier]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 55;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    GetMoneyDetailsCell  * cell = [tableView dequeueReusableCellWithIdentifier:[GetMoneyDetailsCell reuseIdentifier] forIndexPath:indexPath];
    cell.statisLabel.text = dic[@"bill_source"];
    cell.timeLabel.text = dic[@"create_time"];
    cell.moneyLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"bill_amt"]];
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * heardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 55)];
    heardView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView * topView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 45)];
    
    topView.backgroundColor =[UIColor whiteColor];
    [heardView addSubview:topView];
    
    lineView_one =[[UIView alloc]initWithFrame:CGRectMake(20, 43, WIDTH/2-40, 2)];
    lineView_one.backgroundColor = NavBackColor;
    lineView_one.hidden = YES;
    [topView addSubview:lineView_one];
    
    lineView_two =[[UIView alloc]initWithFrame:CGRectMake(WIDTH/2+20, 43, WIDTH/2-40, 2)];
    lineView_two.backgroundColor = NavBackColor;
    lineView_two.hidden = YES;
    [topView addSubview:lineView_two];
    
    getBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    getBtn.frame = CGRectMake(0, 0, WIDTH/2, 43);
    [getBtn setTitle:@"收入明细" forState:UIControlStateNormal];
    
    getBtn.titleLabel.font =[UIFont systemFontOfSize:16];
    [getBtn addTarget:self action:@selector(getBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:getBtn];
    
    outBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    outBtn.frame = CGRectMake(WIDTH/2, 0, WIDTH/2, 43);
    [outBtn setTitle:@"支出明细" forState:UIControlStateNormal];
    if ([typeStr isEqualToString:@"1"]) {
        [getBtn setTitleColor:NavBackColor forState:UIControlStateNormal];
        [outBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        lineView_one.hidden = NO;
        lineView_two.hidden = YES;
        
    }else {
        
        [outBtn setTitleColor:NavBackColor forState:UIControlStateNormal];
        [getBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        lineView_one.hidden = YES;
        lineView_two.hidden = NO;
    }
    
    outBtn.titleLabel.font =[UIFont systemFontOfSize:16];
    [outBtn addTarget:self action:@selector(outBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:outBtn];
    
    return heardView;
}

-(void)getBtnClick:(UIButton *)btn {

    typeStr=@"1";
    type = @"in";
    _page = 1;
      [self getListData];
}

-(void)outBtnClick:(UIButton *)btn {
    
    typeStr=@"2";
    
    type = @"out";
    _page = 1;
    [self getListData];

}

-(void)getListData{
    
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{@"bill_type":type,
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"perpage":@"10",
                           @"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token")
                           };
    
//    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_recordList_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
             _totailPage = [responseObject[@"total_page"]  intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                    noBodyView.hidden =YES;
                    
                    if (_dataArray.count<10) {
                        
                        _tableView.mj_footer.hidden = YES;
                    }
                }else {
                    _tableView.mj_footer.hidden = YES;
                    noBodyView.hidden =NO;
                    
                    [self.view addSubview:noBodyView];
                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}




@end
